package com.parent.utils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
//import org.apache.poi.ss.formula.functions.T;
//import org.springframework.data.elasticsearch.core.SearchHit;

import java.util.List;

/**
 * @Author yan
 * @Date 2023/7/26 0026 17:39
 * @Description 依赖hutool
 */
public class ElasticsechUtils {

    /**
     * List<SearchHit<T>> ==> List<T>
     *
     * @param inputList
     * @param aClass
     * @param <T>
     * @return
     */
//    public static <T,R>  List<T> elasticsechToList(List<R<T>> inputList, Class<T> aClass) {
//        String input = inputList.toString();
//        List<T> list = elasticsechToList(input, aClass);
//        return list;
//    }

    /**
     * List<SearchHit<T>> ===》List<T>
     *
     * @param input
     * @param aClass
     * @param <T>
     * @return
     */
    public static <T> List<T> elasticsechToList(String input, Class<T> aClass) {
        String string = elasticsechToJsonArrayString(input, aClass);
        List<T> list = JSONUtil.toList(string, aClass);
        return list;
    }


    /**
     * List<SearchHit<T>> getContent 类型出现类型转换异常
     * 思路：将List<SearchHit<T>>变成String 然后变成JSON格式 返回String 直接反序列化完成
     *
     * @param input
     * @param aClass
     * @param <T>
     * @return
     */
    public static <T> String elasticsechToJsonArrayString(String input, Class<T> aClass) {
        // String input = "[Blog(id=1690336836067, title=no.4211ca21-4130-49ee-8127-5036831072b1, author=赵梦飞ddf3a92b-0303-4520-8338-e23c79b4441d), Blog(id=1690336876942, title=看来你7aafd8f5-988b-473f-86b4-bdb667f05909, author=赵梦飞7305a1ef-fdc8-4195-a993-b5eac24f54e3)]";
        JSONArray jsonArray = new JSONArray();
        String[] blogs = input.split("\\), ");
        for (String blog : blogs) {
            JSONObject jsonObject = getJsonObject(blog, aClass);
            jsonArray.put(jsonObject);
        }
        String jsonOutput = jsonArray.toString();
        return jsonOutput;
    }

    /**
     * @param input
     * @param aClass
     * @param <T>
     * @return
     */
    public static <T> String elasticsechToJsonObjectString(String input, Class<T> aClass) {
        JSONObject jsonObject = getJsonObject(input, aClass);
        return jsonObject.toString();
    }

    /**
     *
     * @param input
     * @param aClass
     * @return
     * @param <T>
     */
    private static <T> JSONObject getJsonObject(String input, Class<T> aClass) {
        String className = aClass.getSimpleName();
        JSONObject jsonObject = getJsonObject(input, className);
//        String[] fields = input.replace("[" + className + "(", "").replace("]", "").split(", ");
//        JSONObject jsonObject = new JSONObject();
//        for (String field : fields) {
//            String[] keyValue = field.split("=");
//            String key = keyValue[0].trim();
//            String value = keyValue[1].trim();
//            jsonObject.put(key, value);
//        }
        return jsonObject;
    }

    /**
     * @param input
     * @param className
     * @return
     */
    private static JSONObject getJsonObject(String input, String className) {
        String[] fields = input.replace("[", "")
                .replace(className, "")
                .replace("(", "")
                .replace(")", "")
                .replace("]", "")
                .split(",");
        JSONObject jsonObject = new JSONObject();
        for (String field : fields) {
            String[] keyValue = field.split("=");
            String key = keyValue[0].trim();
            String value = keyValue[1].trim();
            jsonObject.put(key, value);
        }
        return jsonObject;
    }
}
